package com.systemframework.sys.service.impl;

import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.SearchResult;
import cn.zhxu.bs.operator.Contain;
import cn.zhxu.bs.util.MapUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.systemframework.sys.dto.request.EmailAccountRequestDTO;
import com.systemframework.sys.dto.response.EmailAccountResponseDTO;
import com.systemframework.sys.entity.EmailAccount;
import com.systemframework.sys.mapper.EmailAccountMapper;
import com.systemframework.sys.service.EmailAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * @author summer
 * @date 2023-04-04 17:34
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class EmailAccountServiceImpl extends ServiceImpl<EmailAccountMapper, EmailAccount> implements EmailAccountService {


    private BeanSearcher beanSearcher;

    @Autowired
    public void setBeanSearcher(BeanSearcher beanSearcher) {
        this.beanSearcher = beanSearcher;
    }

    @Override
    public Page<EmailAccountResponseDTO> lists(EmailAccountRequestDTO emailAccountRequestDTO) {
        Map<String, Object> build = MapUtils.builder().page(emailAccountRequestDTO.getPageNum()-1, emailAccountRequestDTO.getPageSize())
                .field(EmailAccountResponseDTO::getEmail, emailAccountRequestDTO.getEmail()).op(Contain.class)
                .field(EmailAccountResponseDTO::getUsername, emailAccountRequestDTO.getUsername()).op(Contain.class).build();
        SearchResult<EmailAccountResponseDTO> search = beanSearcher.search(EmailAccountResponseDTO.class, build);
        Page<EmailAccountResponseDTO> page=new Page<>(emailAccountRequestDTO.getPageNum(),emailAccountRequestDTO.getPageSize());
        page.setRecords(search.getDataList());
        page.setTotal(search.getTotalCount().longValue());
        return page;
    }
}
